<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 回复
 *
 * @author      Eden
 * @datetime    2016/12/9 00:43
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
class Reply_Model extends API_Model
{
    /**
     * 参数
     * @var array
     */
    private $params = array();

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主函数
     * @param array $params
     */
    public function run(Array $params)
    {
        $this->params = $params;
        $send['state'] = false;
        do {
            if (!$this->_checkParams()) {
                $send['error'] = $this->getError();
                break;
            }
            $reply = $this->saveReply();
            if (empty($reply)) {
                $send['error'] = '回复失败';
            } else {
                $send['state'] = true;
                $send['data'] = $reply;
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 回复
     * @return mixed
     */
    protected function saveReply()
    {
        $replayData = array(
            'commentId' => $this->params['commentId'],
            'userId' => $this->params['userId'],
            'content' => $this->params['content'],
            'created' => dateTime()
        );

        $db = clone($this->db);
        $db->insert('bbs_comments_reply', $replayData);
        if ($id = $db->insert_id()) {

            $com = $this->_getComment();
            if (!$com) {
                return false;
            }
            $comUser = $this->_getNameAndAvatar($com['userId']);
            $userInfo = $this->_getNameAndAvatar($this->params['userId']);
            unset($com['userId']);
            $com['commentId'] = intval($com['commentId']);
            $com['nickName'] = $comUser['nickName'];
            $com['avatar'] = thumb($comUser['avatar'], 100, 100);
            $com['text'] = value($com, 'text', '');
            $com['created'] = formateTimeStamp($com['created']);
            $com['reply'] = array(
                'replayId' => (int) $id,
                'content' => $replayData['content'],
                'created' => formateTimeStamp($replayData['created']),
                'userId' => $userInfo['userId'],
                'nickName' => $userInfo['nickName'],
                'avatar' => thumb($userInfo['avatar'], 100, 100)
            );

            return $com;
        }
        return false;
    }

    /**
     * 获取评论信息
     * @return bool
     */
    private function _getComment()
    {
        $q = $this->db->select('c.id as commentId, c.userId, c.text, c.created')
            ->where(array('c.id' => $this->params['commentId']))
            ->from('bbs_comments as c')
            ->get();
        $row = $q->row_array();
        if (empty($row)) {
            return false;
        }

        return $row;
    }

    /**
     * 检查参数
     * @return bool
     */
    protected function _checkParams()
    {
        if ($this->input->method(true) !== 'POST') {
            $this->setError('非法请求');
            return false;
        }

        if (empty($this->params['commentId']) || empty($this->params['userId']) || empty($this->params['content'])) {
            $this->setError('有必填参数没填');
            return false;
        }

        if (!$this->_checkUserExists($this->params['userId'])) {
            $this->setError('用户不存在');
            return false;
        }

        if (!$this->_checkCommentExists($this->params['commentId'])) {
            $this->setError('评论不存在或已删除');
            return false;
        }
        return true;
    }

}